package com.tzb.shirospringbootjsp.controller;

import com.tzb.shirospringbootjsp.entity.User;
import com.tzb.shirospringbootjsp.service.UserService;
import com.tzb.shirospringbootjsp.utils.VerifyCodeUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Description TODO
 * @Author tzb
 * @Date 2021/8/28 16:27
 * @Version 1.0
 **/
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


    @RequestMapping("/getImage")
    public void getImage(HttpSession session, HttpServletResponse response) throws IOException {
        VerifyCodeUtil.setHeight(60);// 设置验证码高度
        VerifyCodeUtil.setWidth(220);// 设置验证码宽度
        VerifyCodeUtil.setSize(10);// 设置验证码干扰线的条数
        VerifyCodeUtil.setDsize(10);// 设置验证码干扰点的个数
        VerifyCodeUtil.setSize(4);//设置验证码的长度
        //生成验证码
        String code = VerifyCodeUtil.generateVerifyCode();
        //验证码存入 session
        session.setAttribute("code", code);
        //验证码存入图片
        ServletOutputStream os = response.getOutputStream();
        response.setContentType("image/png");
        VerifyCodeUtil.outputImage(os, code);
    }


    /**
     * 用户注册
     *
     * @param user
     * @return
     */
    @RequestMapping("/register")
    public String register(User user) {
        try {
            userService.register(user);
            return "redirect:/login.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            return "redirect:/register.jsp";
        }
    }


    /**
     * 登录
     *
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("/login")
    public String login(String username, String password, String code, HttpSession session) {

        //比较验证码
        String sessionCode = (String) session.getAttribute("code");

        try {
            if (sessionCode.equalsIgnoreCase(code)) {
                // 获取主题对象
                // TODO,在web中，只要配置了安全管理器bean，就会自动注入到安全工具
                Subject subject = SecurityUtils.getSubject();
                subject.login(new UsernamePasswordToken(username, password));
                return "redirect:/index.jsp";
            } else {
                throw new RuntimeException("验证码错误！");
            }

        } catch (UnknownAccountException e) {
            e.printStackTrace();
            System.out.println("用户名错误");
        } catch (IncorrectCredentialsException e) {
            e.printStackTrace();
            System.out.println("密码错误");
        }catch (RuntimeException e){
            e.printStackTrace();
            System.out.println("验证码错误");
        }

        return "redirect:/login.jsp";
    }

    @RequestMapping("/logout")
    public String logout() {
        SecurityUtils.getSubject().logout();
        return "redirect:/login.jsp";
    }

}
